package com.jeesite.modules.kj.api.controller.activity;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.vo.DataListPageVo;
import com.jeesite.modules.kj.api.vo.response.ActivityJobResponseVo;
import com.jeesite.modules.kj.api.vo.response.MemberJobResponseVo;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.entity.KjActivity;
import com.jeesite.modules.kj.entity.KjActivityJob;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.entity.KjMemberJob;
import com.jeesite.modules.kj.enums.CommentEnum;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.service.KjActivityJobService;
import com.jeesite.modules.kj.service.KjActivityService;
import com.jeesite.modules.kj.service.KjMemberJobService;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "活动岗位api", tags = {"活动岗位api"})
@Slf4j
@Controller
@RequestMapping(value = "/api/activityJob")
public class ActivityJobApiController {
    private final static Logger logger = LoggerFactory.getLogger(ActivityJobApiController.class);

    @Autowired
    KjActivityService kjActivityService;

    @Autowired
    KjActivityJobService kjActivityJobService;

    @Autowired
    KjMemberJobService kjMemberJobService;

    @ApiOperation(value = "获取活动的岗位列表", notes = "获取活动的岗位列表")
    @GetMapping(value = "list")
    public ResponseBean<List<ActivityJobResponseVo>> list(String activityId) {
        KjActivityJob where = new KjActivityJob();
        where.setActivityId(activityId);
        List<KjActivityJob> dataList = kjActivityJobService.findList(where);
        List<ActivityJobResponseVo> vos = ListUtils.newArrayList();
        for (KjActivityJob activityJob: dataList
        ) {
            vos.add(new ActivityJobResponseVo(activityJob));
        }
        return new ResponseBean<>(vos);
    }

    @ApiOperation(value = "获取岗位报名情况列表", notes = "获取岗位报名情况列表")
    @GetMapping(value = "memberList")
    public ResponseBean<DataListPageVo<MemberJobResponseVo>> memberList(String activityId, String approveStatus, int pageNo, int pageSize) {
        KjMemberJob where = new KjMemberJob();
        where.setActivityId(activityId);
        where.setStatus(KjMemberJob.STATUS_NORMAL);
        if(StringUtils.isNotBlank(approveStatus)) {
            where.setApproveStatus(approveStatus);
        }else{
            where.setApproveStatusNotIn(new String[]{KjMemberJob.FINISH});
        }
        Page<KjMemberJob> page = new Page(pageNo, pageSize);
        where.setPage(page);
        Page<KjMemberJob> dataList = kjMemberJobService.findPage(where);
        DataListPageVo<MemberJobResponseVo> result = new DataListPageVo<>(dataList, MemberJobResponseVo.class);
        return new ResponseBean<>(result);
    }

    @ApiOperation(value = "报名参加活动", notes = "报名参加活动")
    @PostMapping(value = "attention")
    @UserLoginToken
    public ResponseBean<List<MemberJobResponseVo>> attention(@ApiIgnore @CurrentUser String memberId, String jobIds, String apply) throws FreezMemberException {

        KjMember currentMember = KjMemberUtil.getCurrentMember(memberId);
        if(currentMember == null){
            return new ResponseBean(Global.FALSE, "用户不存在");
        }
        KjActivityJob where = new KjActivityJob();
        where.setId_in(jobIds.split(","));
        List<KjActivityJob> dataList = kjActivityJobService.findList(where);
        for (KjActivityJob activityJob: dataList
        ) {
            KjMemberJob jobWhere = new KjMemberJob();
            jobWhere.setKjMember(currentMember);
            jobWhere.setJobId(activityJob.getId());
            List<KjMemberJob> attentions = kjMemberJobService.findList(jobWhere);
            if(attentions.size() == 0){
                KjActivity activity = kjActivityService.get(activityJob.getActivityId());
                jobWhere.setJobName(activityJob.getJobName());
                jobWhere.setActivityName(activityJob.getActivityName());
                jobWhere.setActivityId(activityJob.getActivityId());
                jobWhere.setKjMember(currentMember);
                jobWhere.setCityId(activity.getKjCity().getId());
                jobWhere.setCityName(activity.getKjCity().getName());
                jobWhere.setApply(apply);
                kjMemberJobService.save(jobWhere);
                kjActivityJobService.updateAttentions(activityJob.getId());

            }else{
                for (KjMemberJob attention: attentions
                     ) {
                    if(attention.getApproveStatus().equals(KjMemberJob.REJECT)){
                        attention.setApproveStatus(KjMemberJob.PROCESSING);
                        kjMemberJobService.save(attention);
                    }
                }
                kjActivityJobService.updateAttentions(activityJob.getId());
            }
        }
        KjMemberJob where2 = new KjMemberJob();
        KjMember curMember = KjMemberUtil.getCurrentMember(memberId);
        where2.setActivityId(dataList.get(0).getActivityId());
        where2.setKjMember(curMember);
        List<KjMemberJob> dataList2 = kjMemberJobService.findList(where2);
        List<MemberJobResponseVo> vos = ListUtils.newArrayList();
        for (KjMemberJob memberJob: dataList2
        ) {
            vos.add(new MemberJobResponseVo(memberJob));
        }
        return new ResponseBean<>(vos);
    }



    @ApiOperation(value = "结束报名", notes = "结束报名")
    @PostMapping(value = "finish")
    @UserLoginToken
    public ResponseBean<String> finish(@ApiIgnore @CurrentUser String memberId, String activityId) throws FreezMemberException {
        KjMemberJob where = new KjMemberJob();
        KjMember curMember = KjMemberUtil.getCurrentMember(memberId);
        where.setActivityId(activityId);
        where.setKjMember(curMember);
        List<KjMemberJob> dataList = kjMemberJobService.findList(where);
        for (KjMemberJob memberJob: dataList
        ) {
            if(memberJob.getApproveStatus().equals(KjMemberJob.REJECT)){
                memberJob.setApproveStatus(KjMemberJob.FINISH);
                kjMemberJobService.save(memberJob);
                kjActivityJobService.updateAttentions(memberJob.getJobId());
            }
        }
        return new ResponseBean<>("已结束被拒绝的报名");
    }



    @ApiOperation(value = "我参加的岗位", notes = "我参加的岗位")
    @PostMapping(value = "my")
    @UserLoginToken
    public ResponseBean<List<MemberJobResponseVo>> my(@ApiIgnore @CurrentUser String memberId, String activityId) throws FreezMemberException {
        KjMemberJob where = new KjMemberJob();
        KjMember curMember = KjMemberUtil.getCurrentMember(memberId);
        where.setActivityId(activityId);
        where.setKjMember(curMember);
        List<KjMemberJob> dataList = kjMemberJobService.findList(where);
        List<MemberJobResponseVo> vos = ListUtils.newArrayList();
        for (KjMemberJob memberJob: dataList
        ) {
            vos.add(new MemberJobResponseVo(memberJob));
        }
        return new ResponseBean<>(vos);
    }


}
